package org.al.poc.sockets;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SocketManager {

	protected Socket socket;
	
	
	public SocketManager(Socket socket) {
		this.socket = socket;
	}
	
	
	
	/**
	 * @return
	 */
	public Object readObject() throws IOException {

		InputStream is = null;
		ObjectInputStream ois = null;
		Object object = null;
		
//		try {
			
			is = socket.getInputStream();
			ois = new ObjectInputStream(is);
			
			try {
				object = ois.readObject();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
//		finally {
//			if (ois != null)
//				try { ois.close(); }
//				catch (Exception e1) { e1.printStackTrace(); }
//			if (is != null)
//				try { is.close(); }
//				catch (Exception e1) { e1.printStackTrace(); }
//		}

		return object;
	}
	
	
	/**
	 * @param object
	 */
	public void writeObject (Object object) throws IOException {
		OutputStream os = null;
		ObjectOutputStream oos = null;
		
//		try {
			
			os = socket.getOutputStream();
			oos = new ObjectOutputStream(os);
			oos.writeObject(object);
			
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
//		finally {
//			if (oos != null)
//				try { oos.close(); }
//				catch (Exception e1) { e1.printStackTrace(); }
//			if (os != null)
//				try { os.close(); }
//				catch (Exception e1) { e1.printStackTrace(); }
//		}
	}
	
}
